//
//  PictureViewCell.m
//  UI_Project_A
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 GH. All rights reserved.
//

#import "PictureViewCell.h"
#import "PictureTableViewCell.h"
#import "HandleData.h"
#import "PictureModel.h"
#import "TableViewCellOfTwo.h"
#import "TableViewCellOfFour.h"
#import <MJRefresh.h>
#import <DKNightVersion.h>


@interface PictureViewCell ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,retain)UITableView *tableview;
@property (nonatomic ,retain)NSMutableArray *arrLabel;
@property (nonatomic ,retain)NSString *strNid;
@property (nonatomic ,retain)NSMutableArray *arrNid;

@end


@implementation PictureViewCell


- (void)dealloc {

    [_arrNid release];
    [_strNid release];
    [_tableview release];
    [_arrLabel release];
    [super dealloc];



}



- (instancetype)initWithFrame:(CGRect)frame {


    self = [super initWithFrame:frame];
    if (self) {
       
        [self createTableview];
        [self data];
        [self refresh];
    }
  
    
    
    
    
    return self;
}

- (void)data {
    
    
    self.strNid = @"http://api.baiyue.baidu.com/sn/api/medianewsinfo?type=image&subtype=1&nids=";
    self.arrNid = [NSMutableArray array];

    self.arrLabel = [NSMutableArray array];
 [HandleData handleFloewNewWithString:@"http://api.baiyue.baidu.com/sn/api/medianewslist?type=image&subtype=1&ln=200&an=10&wf=1&pd=newsplus&platform=andriod&ver=1" Block:^(id result) {
     
       NSDictionary * dicData = [result objectForKey:@"data"];
      NSArray *arr =  [dicData objectForKey:@"news"];
     
     for (NSDictionary *dicNid in arr) {
        
      NSString * str = [dicNid objectForKey:@"nid"];
         [self.arrNid addObject:str];
         
     }
     for (int i = 0; i < 40; i++) {
         
              self.strNid = [self.strNid stringByAppendingFormat:@"%@,",self.arrNid[i]];
     }
     
     
     self.strNid = [self.strNid stringByAppendingString:@"&pd=newsplus&platform=andriod&ver=1"];
     
     
     
     [HandleData handleFloewNewWithString:self.strNid Block:^(id result) {
         
         NSDictionary * dicData1 = [result objectForKey:@"data"];
         NSArray *arr1 =  [dicData1 objectForKey:@"news"];
         
         
     for (NSDictionary *dic in arr1) {
         
         
         if ([dic objectForKey:@"title"]) {
         
         PictureModel *model = [[PictureModel alloc] init];
         
    
         model.title = [dic objectForKey:@"title"];
             
         model.imageurls = [dic objectForKey:@"imageurls"];
             model.content = [dic objectForKey:@"content"];
             
             [self.arrLabel addObject:model];
             
        
        }
         
     }
     
         [self.tableview reloadData];
     }];
 }];


}


#pragma mark - 刷新
- (void)refresh {
  
        
        
self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
    [self data];
    [self.tableview reloadData];

    [self.tableview.mj_header endRefreshing];

    
        }];




}






#pragma mark - talbeview

- (void)createTableview {

    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
    [self.contentView addSubview:self.tableview];
    [self.tableview release];
    self.tableview.dataSource = self;
    self.tableview.delegate  = self;

    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableview registerClass:[PictureTableViewCell class] forCellReuseIdentifier:@"pool"];
    [self.tableview registerClass:[TableViewCellOfTwo class] forCellReuseIdentifier:@"pool2"];
    [self.tableview registerClass:[TableViewCellOfFour class] forCellReuseIdentifier:@"pool4"];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {



    return self.arrLabel.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    
   PictureModel *model = [self.arrLabel objectAtIndex:indexPath.row];
        
    if (model.imageurls.count == 4) {
        
    TableViewCellOfFour *cell = [tableView dequeueReusableCellWithIdentifier:@"pool4"];
        [cell passModelOfPic:model];
        cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],[UIColor colorWithWhite:0.247 alpha:1.000]);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

   
    return cell;
    }else if (model.imageurls.count == 3){
        TableViewCellOfTwo *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
    
        [cell passModelWithPic:model];
        cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],[UIColor colorWithWhite:0.247 alpha:1.000]);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }else {
    
        PictureTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
        
        
        [cell passModelWithPic:model];
        cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],[UIColor colorWithWhite:0.247 alpha:1.000]);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    
    }
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {


    return self.frame.size.height / 2 + 20;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    
    
    PictureModel *model = [self.arrLabel objectAtIndex:indexPath.row];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"picture" object:nil userInfo:@{@"content":model}];



}







@end
